/**
 *  * @author QinGuan
 *  * @date:2016-1-20 下午03:18:15
 *  * @version :3.0
 *  *
 *  
 */
package com.ctvit.wsservice.basedata.service;

import com.ctvit.framework.dao.Condition;
import com.ctvit.framework.service.BaseService;
import com.ctvit.framework.util.HttpUtils;
import com.ctvit.framework.web.context.ContextHolder;
import com.ctvit.wsservice.basedata.domain.ZcdmTChanl;
import com.ctvit.wsservice.basedata.domain.ZcdmTFinalitemWithBLOBs;
import org.apache.log4j.Logger;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@SuppressWarnings("serial")
public class ChannelInfoService extends BaseService {

    private Properties properties = new Properties();

    private static final Logger LOGGER = Logger.getLogger(ChannelInfoService.class);

    /**
     * @param zcdmTChanl频道类
     * @return
     * @ps:保存频道内容到频道表
     */
    public int saveChannel(ZcdmTChanl zcdmTChanl) {
        Condition condition = new Condition();
        condition.add("zChanlCd", zcdmTChanl.getZChanlCd());
        int i = genericDao.updateByCondition(zcdmTChanl, null, condition);
        //频道信息下发8K运管平台
        Map<String, Object> channel = convertPoToMap(zcdmTChanl);
        JSONObject channelJson = new JSONObject();
        channelJson.put("cdmChannelDTO", channel);
        try {
            InputStream inputStream = ContextHolder.class.getResourceAsStream("/config/yygl.properties");
            properties.load(inputStream);

            String channelYyglUrl = properties.getProperty("yygl.channelYyglUrl");
            String method = properties.getProperty("yygl.method");
            Integer tryNum = Integer.parseInt(properties.getProperty("yygl.tryNum"));
            HttpUtils.JsonHttpRequestPost(channelYyglUrl, method, channelJson, tryNum);
        } catch (IOException e) {
            LOGGER.error("频道信息下发运管平台失败", e);
        }
        if (i == 0) {
            genericDao.insert(zcdmTChanl);// 保存
            return 0;
        }
        return 1;
    }

    private Map<String, Object> convertPoToMap(ZcdmTChanl zcdmTChanl) {
        Map<String, Object> map = new HashMap<>();
        map.put("chanelCode", zcdmTChanl.getZChanlCd());
        map.put("channelName", zcdmTChanl.getZChanlName());
        map.put("channelType", zcdmTChanl.getZChanlType());
        map.put("channelStatus", zcdmTChanl.getZChanlStatus());
        return map;
    }

}
